package com.acme.bankapp;

import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.network.BankServer;
import com.acme.bankapp.service.*;

/**
 * 
 * Holds the Bank data container and the Service classes that perform operations on Bank.
 * 
 */
public class BankApplication {

	public static void main(String... args) {
		boolean serverMode = false;

		System.out.println("--- [BankApplication] Startup...");

		Bank bank = new Bank();
		BankIOService bIOService = new BankIOService(bank);

		if (args.length > 0) {
			int i = 0;
			while (i < args.length){
				if ("-loadfeed".equals(args[i])) {
					System.out.println("\n\n--- [BankApplication] Loading bank from feed...");				
					bank = bIOService.loadBankFromFeedFile(args[++i]);
				} else if ("-loadbank".equals(args[i])) {
					System.out.println("\n--- [BankApplication] Deserializing bank...");
					bank = bIOService.readBank(args[++i]);
				} else if ("-server".equals(args[i])) {
					serverMode = true;
				}
				i++;
			}
		}
		bIOService.bank = bank;
		
		System.out.println("--- [BankApplication] Initialization OK. Backing up...");
		bIOService.writeBank("bank.ser");

		ClientService clientService = new ClientService(bank);
		AccountService accountService = new AccountService(bank);
		BankIOService.showAllAccounts(bIOService.bank);

		if (serverMode) {
			System.out.println("--- [BankApplication] Running server...\n");
			(new Thread(new BankServer(bank, clientService, accountService, bIOService))).start();
		}
				
		System.out.println("--- [BankApplication] Terminating...\n");
	}

}
